<?php

/**
*+------------IMBA收藏业务数据的操作---------------+
*+-------------------------------------------------+
*+--------------用于提供内部接口-------------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/

namespace Api\Event;

class FavourEvent extends BaseEvent 
{	
	/**
	*模型对象
	*/
	private $servObj = '';

	/**
	*基础参数的初始化
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	protected function _initialize(){
		$this->servObj = D('Common/Favour', 'Service');
	}

	/**
	*用户收藏
	*
	*@param int $uid 操作的用户标识
	*@param int $vid 视频标识
	*@return array
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	public function add($uid, $vid){

		$vtype = $this->checkParam($vid);
		if($vtype === false){
			return array('code'=>self::$API_ERROR['VIDEO'], 'info'=>'收藏对象信息错误');
		}
		/*$map = array(
				'video_id'=>intval($vid), 
				'uid'=>intval($uid)
				);
		if($serviceObj->where($map)->find())
			return array('code'=>self::$API_ERROR['FAVOUR'], 'info'=>'目标已收藏');*/
		if($this->isFavour($uid, $vid)){
			return array('code'=>self::$API_ERROR['FAVOUR'], 'info'=>'目标已收藏');
		}
		
		$serviceObj = $this->servObj;
		$data = array('uid'=>$uid, 'video_id'=>$vid);
		if($vtype)
			$data['type'] = $serviceObj::TYPE_MATCH;
		else
			$data['type'] = $serviceObj::TYPE_VIDEO;

		$data = $serviceObj->create($data);
		if(!$data){
			$errmsg = $serviceObj->getError();
			return array('code'=>self::$API_ERROR['FAVOUR_ILLEGAL'], 'info'=>$errmsg);
		}

 		$res = $serviceObj->add($data);
 		if($res){
 			service('Video')->where(['id' => $vid])->setInc('favour', 1);
 			event('Statis')->operStatisVideo($vid, $uid, 'favour');
			return array('code'=>self::$API_ERROR['SUCCESS'], 'info'=>'收藏成功');
		}else{
			return array('code'=>self::$API_ERROR['SERVER'], 'info'=>'未知错误');
		}
	}

	/**
	*用户取消收藏
	*@param int $uid 操作的用户标识
	*@param int $vid 视频标识
	*@return array
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	public function undo($uid, $vid){

		//$servObj = D('Common/Favour', 'Service');
		/*$map = array(
				'video_id'=>intval($vid), 
				'uid'=>intval($uid)
				);

		if(!$this->servObj->where($map)->find())
			return array('code'=>self::$API_ERROR['NO_FAVOUR'], 'info'=>'还未收藏');*/
		if($this->isFavour($uid, $vid) === false)
			return array('code'=>self::$API_ERROR['NO_FAVOUR'], 'info'=>'还未收藏');

		$res = $this->servObj->where($map)->delete();
		if($res){
			service('Video')->where(['id' => $vid])->setDec('favour', 1);
			event('Statis')->operStatisVideo($video_id, $user['uid'], 'favour', -1);
			return array('code'=>self::$API_ERROR['SUCCESS'], 'info'=>'取消收藏成功');
		}else{
			return array('code'=>self::$API_ERROR['SERVER'], 'info'=>'未知错误');
		}
	}

	/**
	*
	*目标参数对象检查
	*/
	private function checkParam($vid){

		$vdObj = A('Api/Video', 'Event');

		$info = $vdObj->info($vid);

		if(!$info)
			return false;
		else
			return $info['type'];

	}

	/**
	*获取用户的收藏信息
	*@param int $uid 操作的用户
	*@param int $timestamp 时间戳
	*@return array 
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	public function get($uid, $p=1, $limit=0, $type=0){

		$map = array(
				'uid'=>intval($uid),
				'type'=>intval($type)
			);

		$res = $this->servObj->where($map);
		if($limit)
			$res = $res->limit($limit);
		if($p)
			$res = $res->page($p);
		//$ids = $res->getField('video_id', true);
		$data = $res->order('create_time DESC')->field('video_id,create_time')->select();

		return $data;
	}

	/**
	*获取用户收藏总数
	*@param int $uid 用户标识
	*@return int
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	public function uTotal($uid, $type=0){

		$map = array(
				'uid'=>intval($uid),
				'type'=>intval($type)
				);

		return $this->servObj->where($map)->count();
	}

	/**
	*视频被收藏的总数
	*@param int $vid 视频标识
	*@return int 
	*@author susy.liang <liangling@ilongyuan.com.cn>
	*/
	public function vTotal($vid){
		$map = array('video_id'=>intval($vid));
		return $this->servObj->where($map)->count();
	}

	/**
	*用户是否收藏
	*@param int $uid 用户标识
	*@param int $vid 视频或比赛标识
	*@author susyliang<liangling@ilongyuan.com.cn>
	*/
	public function isFavour($uid, $vid){

		$map = array(
				'video_id'=>intval($vid), 
				'uid'=>intval($uid)
				);
		$res = $this->servObj->where($map)->find();

		return $res?true:false;

	}

	public function lists($query = [], $limit = 0, $offset = 0){
		$res = $this->servObj->where($query);
		$limit && $res->limit($offset, $limit);

		$data = $res->order('create_time DESC')->field('video_id,create_time')->select();
		return $data;
	}
}